﻿using Application.DTO;
using Application.DTO.Role;
using Application.IService.Role;
using Domain.Entity.Role;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;

namespace ApiServer.Controllers.Role
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("role")]
    [ApiController]
    public class RoleController : BaseController<RoleInfo>
    {
        private readonly IRoleService roleService;
        public RoleController(IRoleService roleService) : base(roleService)
        {
            this.roleService = roleService;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> RoleAdd(RoleAddRequestDTO dto)
        {
            //判断前端参数是否符合模型验证
            if (!ModelState.IsValid)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.PARAMSERROR,
                    Message = "参数错误！！！",
                    Data = null,
                });
            }
            return Ok(await roleService.RoleAdd(dto));
        }

        /// <summary> 
        /// 修改角色信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<IActionResult> RoleUpdate(RoleUpdateRequestDTO dto)
        {
            //判断前端参数是否符合模型验证
            if (!ModelState.IsValid)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.PARAMSERROR,
                    Message = "添加失败！！！",
                    Data = null,
                });
            }
            return Ok(await roleService.RoleUpdate(dto));
        }

        /// <summary>
        /// 角色的分页显示+查询
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("select")]
        public async Task<IActionResult> RolePageShow(int pageSize = 999, int pageIndex = 1, string? name = "")
        {
            //判断前端参数是否符合模型验证
            if (!ModelState.IsValid)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.PARAMSERROR,
                    Message = "查询失败！！！",
                    Data = null,
                });
            }
            var n = await roleService.RolePageShow(pageSize, pageIndex, name);
            return Ok(n);
        }

        /// <summary>
        /// 单个角色的详细显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("SelectById")]
        public async Task<IActionResult> RoleSelectById(int id)
        {
            if (id == null)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.ENTITYNULL,
                    Message = "未接收到id值",
                    Data = null,
                });
            }
            return Ok(await roleService.RoleSelectById(id));
        }

        /// <summary>
        /// 逻辑删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("softDelete")]
        public async Task<IActionResult> RoleSoftDel(int id)
        {
            if (id == null)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.ENTITYNULL,
                    Message = "未接收到id值",
                    Data = null,
                });
            }
            return Ok(await roleService.RoleSoftDel(id));
        }

        /// <summary>
        /// 角色的批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("softDelAll")]
        public async Task<IActionResult> RoleSoftDelAll(string idList)
        {
            return Ok(await roleService.RoleSoftDelAll(idList));
        }

        /// <summary>
        /// 角色的真删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<IActionResult> RoleDel(RoleDelDTO dto)
        {
            //判断前端参数是否符合模型验证
            if (!ModelState.IsValid)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.PARAMSERROR,
                    Message = "操作失败！！！",
                    Data = null,
                });
            }
            return Ok(await roleService.RoleDel(dto));
        }

        /// <summary>
        /// 角色的批量真删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("RoleDelAll")]
        public async Task<IActionResult> RoleDelAll(string idList)
        {
            return Ok(await roleService.RoleDelAll(idList));
        }

        /// <summary>
        /// 角色分配权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("permissionAllot")]
        public async Task<IActionResult> RolePermissionAllot(PermissionAllotDTO dto)
        {
            //判断前端参数是否符合模型验证
            if (!ModelState.IsValid)
            {
                return Ok(new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.PARAMSERROR,
                    Message = "操作失败！！！",
                    Data = null,
                });
            }
            return Ok(await roleService.RolePermissionAllot(dto));
        }
    }
}
